<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: samples/cpp/grabcut.cpp</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">samples/cpp/grabcut.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<p>An example using the GrabCut algorithm </p><div class="image">
<img src="../../grabcut_output1.jpg" alt="grabcut_output1.jpg"/>
<div class="caption">
Sample Screenshot</div></div>
 <div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d6/d87/imgcodecs_8hpp.html">opencv2/imgcodecs.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d8/dcc/namespacestd.html">std</a>;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> help(<span class="keywordtype">char</span>** argv)</div><div class="line">{</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;\nThis program demonstrates GrabCut segmentation -- select an object in a region\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;and then grabcut will attempt to segment it out.\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;Call:\n&quot;</span></div><div class="line">        &lt;&lt;  argv[0] &lt;&lt; <span class="stringliteral">&quot; &lt;image_name&gt;\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\nSelect a rectangular area around the object you want to segment\n&quot;</span> &lt;&lt;</div><div class="line">            <span class="stringliteral">&quot;\nHot keys: \n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\tESC - quit the program\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\tr - restore the original image\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\tn - next iteration\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\tleft mouse button - set rectangle\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\tCTRL+left mouse button - set GC_BGD pixels\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\tSHIFT+left mouse button - set GC_FGD pixels\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\tCTRL+right mouse button - set GC_PR_BGD pixels\n&quot;</span></div><div class="line">            <span class="stringliteral">&quot;\tSHIFT+right mouse button - set GC_PR_FGD pixels\n&quot;</span> &lt;&lt; endl;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">const</span> <a name="_a0"></a><a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a> RED = <a name="a1"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0,0,255);</div><div class="line"><span class="keyword">const</span> <a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a> PINK = <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(230,130,255);</div><div class="line"><span class="keyword">const</span> <a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a> BLUE = <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,0,0);</div><div class="line"><span class="keyword">const</span> <a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a> LIGHTBLUE = <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(255,255,160);</div><div class="line"><span class="keyword">const</span> <a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a> GREEN = <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(0,255,0);</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> BGD_KEY = <a name="a2"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggaab4dc057947f70058c80626c9f1c25cea5876848aeb4e7cc282d461f94e45a07f">EVENT_FLAG_CTRLKEY</a>;</div><div class="line"><span class="keyword">const</span> <span class="keywordtype">int</span> FGD_KEY = <a name="a3"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggaab4dc057947f70058c80626c9f1c25cea61da926b22fd4fd3aa226ef1d2893102">EVENT_FLAG_SHIFTKEY</a>;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> getBinMask( <span class="keyword">const</span> <a name="_a4"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; comMask, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; binMask )</div><div class="line">{</div><div class="line">    <span class="keywordflow">if</span>( comMask.<a name="a5"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() || comMask.<a name="a6"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#af2d2652e552d7de635988f18a84b53e5">type</a>()!=<a name="a7"></a><a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga81df635441b21f532fdace401e04f588">CV_8UC1</a> )</div><div class="line">        <a name="a8"></a><a class="code" href="../../db/de0/group__core__utils.html#ga5b48c333c777666e076bd7052799f891">CV_Error</a>( <a name="a9"></a><a class="code" href="../../d1/d0d/namespacecv_1_1Error.html#a759fa1af92f7aa7377c76ffb142abccaaf587497af64537041ee62c04a92b755d">Error::StsBadArg</a>, <span class="stringliteral">&quot;comMask is empty or has incorrect type (not CV_8UC1)&quot;</span> );</div><div class="line">    <span class="keywordflow">if</span>( binMask.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() || binMask.<a name="a10"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>!=comMask.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a> || binMask.<a name="a11"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>!=comMask.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> )</div><div class="line">        binMask.<a name="a12"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#a55ced2c8d844d683ea9a725c60037ad0">create</a>( comMask.<a name="a13"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#a146f8e8dda07d1365a575ab83d9828d1">size</a>(), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga81df635441b21f532fdace401e04f588">CV_8UC1</a> );</div><div class="line">    binMask = comMask &amp; 1;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keyword">class </span>GCApplication</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line">    <span class="keyword">enum</span>{ NOT_SET = 0, IN_PROCESS = 1, SET = 2 };</div><div class="line">    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> radius = 2;</div><div class="line">    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> thickness = -1;</div><div class="line"></div><div class="line">    <span class="keywordtype">void</span> reset();</div><div class="line">    <span class="keywordtype">void</span> setImageAndWinName( <span class="keyword">const</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; _image, <span class="keyword">const</span> <span class="keywordtype">string</span>&amp; _winName );</div><div class="line">    <span class="keywordtype">void</span> <a name="a14"></a><a class="code" href="../../d7/d84/namespacecvv_1_1impl.html#a5e7f1938e252a7c78ab0fa6751f06808">showImage</a>() <span class="keyword">const</span>;</div><div class="line">    <span class="keywordtype">void</span> mouseClick( <span class="keywordtype">int</span> event, <span class="keywordtype">int</span> x, <span class="keywordtype">int</span> y, <span class="keywordtype">int</span> flags, <span class="keywordtype">void</span>* param );</div><div class="line">    <span class="keywordtype">int</span> nextIter();</div><div class="line">    <span class="keywordtype">int</span> getIterCount()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> iterCount; }</div><div class="line"><span class="keyword">private</span>:</div><div class="line">    <span class="keywordtype">void</span> setRectInMask();</div><div class="line">    <span class="keywordtype">void</span> setLblsInMask( <span class="keywordtype">int</span> flags, <a name="_a15"></a><a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a> p, <span class="keywordtype">bool</span> isPr );</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">string</span>* winName;</div><div class="line">    <span class="keyword">const</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>* image;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> <a name="a16"></a><a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> bgdModel, fgdModel;</div><div class="line"></div><div class="line">    <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga65f85814a8290f9797005d3b28e7e5fc">uchar</a> rectState, lblsState, prLblsState;</div><div class="line">    <span class="keywordtype">bool</span> isInitialized;</div><div class="line"></div><div class="line">    <a name="_a17"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> rect;</div><div class="line">    vector&lt;Point&gt; fgdPxls, bgdPxls, prFgdPxls, prBgdPxls;</div><div class="line">    <span class="keywordtype">int</span> iterCount;</div><div class="line">};</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> GCApplication::reset()</div><div class="line">{</div><div class="line">    <span class="keywordflow">if</span>( !<a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>.empty() )</div><div class="line">        <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>.setTo(Scalar::all(<a name="a18"></a><a class="code" href="../../d7/d1b/group__imgproc__misc.html#ggad43d3e4208d3cf025d8304156b02ba38a889f1ce109543e8aed80a7abbc6dcb39">GC_BGD</a>));</div><div class="line">    bgdPxls.clear(); fgdPxls.clear();</div><div class="line">    prBgdPxls.clear();  prFgdPxls.clear();</div><div class="line"></div><div class="line">    isInitialized = <span class="keyword">false</span>;</div><div class="line">    rectState = NOT_SET;</div><div class="line">    lblsState = NOT_SET;</div><div class="line">    prLblsState = NOT_SET;</div><div class="line">    iterCount = 0;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> GCApplication::setImageAndWinName( <span class="keyword">const</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; _image, <span class="keyword">const</span> <span class="keywordtype">string</span>&amp; _winName  )</div><div class="line">{</div><div class="line">    <span class="keywordflow">if</span>( _image.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() || _winName.empty() )</div><div class="line">        <span class="keywordflow">return</span>;</div><div class="line">    image = &amp;_image;</div><div class="line">    winName = &amp;_winName;</div><div class="line">    <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>.create( image-&gt;size(), <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga81df635441b21f532fdace401e04f588">CV_8UC1</a>);</div><div class="line">    reset();</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> <a class="code" href="../../d7/d84/namespacecvv_1_1impl.html#a5e7f1938e252a7c78ab0fa6751f06808">GCApplication::showImage</a>()<span class="keyword"> const</span></div><div class="line"><span class="keyword"></span>{</div><div class="line">    <span class="keywordflow">if</span>( image-&gt;empty() || winName-&gt;empty() )</div><div class="line">        <span class="keywordflow">return</span>;</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> res;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> binMask;</div><div class="line">    <span class="keywordflow">if</span>( !isInitialized )</div><div class="line">        image-&gt;<a name="a19"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#a33fd5d125b4c302b0c9aa86980791a77">copyTo</a>( res );</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        getBinMask( <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>, binMask );</div><div class="line">        image-&gt;copyTo( res, binMask );</div><div class="line">    }</div><div class="line"></div><div class="line">    vector&lt;Point&gt;::const_iterator it;</div><div class="line">    <span class="keywordflow">for</span>( it = bgdPxls.begin(); it != bgdPxls.end(); ++it )</div><div class="line">        <a name="a20"></a><a class="code" href="../../d9/db7/group__datasets__gr.html#gga610754124ced68d1f05760b5948fbb76a6f0d8b2d9e3e947b2a5c1eff9e81ee95">circle</a>( res, *it, radius, BLUE, thickness );</div><div class="line">    <span class="keywordflow">for</span>( it = fgdPxls.begin(); it != fgdPxls.end(); ++it )</div><div class="line">        <a class="code" href="../../d9/db7/group__datasets__gr.html#gga610754124ced68d1f05760b5948fbb76a6f0d8b2d9e3e947b2a5c1eff9e81ee95">circle</a>( res, *it, radius, RED, thickness );</div><div class="line">    <span class="keywordflow">for</span>( it = prBgdPxls.begin(); it != prBgdPxls.end(); ++it )</div><div class="line">        <a class="code" href="../../d9/db7/group__datasets__gr.html#gga610754124ced68d1f05760b5948fbb76a6f0d8b2d9e3e947b2a5c1eff9e81ee95">circle</a>( res, *it, radius, LIGHTBLUE, thickness );</div><div class="line">    <span class="keywordflow">for</span>( it = prFgdPxls.begin(); it != prFgdPxls.end(); ++it )</div><div class="line">        <a class="code" href="../../d9/db7/group__datasets__gr.html#gga610754124ced68d1f05760b5948fbb76a6f0d8b2d9e3e947b2a5c1eff9e81ee95">circle</a>( res, *it, radius, PINK, thickness );</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>( rectState == IN_PROCESS || rectState == SET )</div><div class="line">        <a name="a21"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>( res, <a name="a22"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>( rect.x, rect.y ), <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a>(rect.x + rect.width, rect.y + rect.height ), GREEN, 2);</div><div class="line"></div><div class="line">    <a name="a23"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( *winName, res );</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> GCApplication::setRectInMask()</div><div class="line">{</div><div class="line">    <a name="a24"></a><a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>( !<a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>.empty() );</div><div class="line">    <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>.setTo( <a class="code" href="../../d7/d1b/group__imgproc__misc.html#ggad43d3e4208d3cf025d8304156b02ba38a889f1ce109543e8aed80a7abbc6dcb39">GC_BGD</a> );</div><div class="line">    rect.x = <a name="a25"></a><a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#ga78f988f6cfa6223610298cbd4f86ec66">max</a>(0, rect.x);</div><div class="line">    rect.y = <a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#ga78f988f6cfa6223610298cbd4f86ec66">max</a>(0, rect.y);</div><div class="line">    rect.width = <a name="a26"></a><a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#gac48df53b8fd34b87e7b121fa8fd4c379">min</a>(rect.width, image-&gt;cols-rect.x);</div><div class="line">    rect.height = <a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#gac48df53b8fd34b87e7b121fa8fd4c379">min</a>(rect.height, image-&gt;rows-rect.y);</div><div class="line">    (<a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>(rect)).setTo( <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>(<a name="a27"></a><a class="code" href="../../d7/d1b/group__imgproc__misc.html#ggad43d3e4208d3cf025d8304156b02ba38ad33184b73cb87e08d29e0a3411b7c863">GC_PR_FGD</a>) );</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> GCApplication::setLblsInMask( <span class="keywordtype">int</span> flags, <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a> p, <span class="keywordtype">bool</span> isPr )</div><div class="line">{</div><div class="line">    vector&lt;Point&gt; *bpxls, *fpxls;</div><div class="line">    <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga65f85814a8290f9797005d3b28e7e5fc">uchar</a> bvalue, fvalue;</div><div class="line">    <span class="keywordflow">if</span>( !isPr )</div><div class="line">    {</div><div class="line">        bpxls = &amp;bgdPxls;</div><div class="line">        fpxls = &amp;fgdPxls;</div><div class="line">        bvalue = <a class="code" href="../../d7/d1b/group__imgproc__misc.html#ggad43d3e4208d3cf025d8304156b02ba38a889f1ce109543e8aed80a7abbc6dcb39">GC_BGD</a>;</div><div class="line">        fvalue = <a name="a28"></a><a class="code" href="../../d7/d1b/group__imgproc__misc.html#ggad43d3e4208d3cf025d8304156b02ba38a4757c1f0587bcf6e53e86dee7689a649">GC_FGD</a>;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        bpxls = &amp;prBgdPxls;</div><div class="line">        fpxls = &amp;prFgdPxls;</div><div class="line">        bvalue = <a name="a29"></a><a class="code" href="../../d7/d1b/group__imgproc__misc.html#ggad43d3e4208d3cf025d8304156b02ba38af748414821c7f39fab3493f9eed1eedf">GC_PR_BGD</a>;</div><div class="line">        fvalue = <a class="code" href="../../d7/d1b/group__imgproc__misc.html#ggad43d3e4208d3cf025d8304156b02ba38ad33184b73cb87e08d29e0a3411b7c863">GC_PR_FGD</a>;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">if</span>( flags &amp; BGD_KEY )</div><div class="line">    {</div><div class="line">        bpxls-&gt;push_back(p);</div><div class="line">        <a class="code" href="../../d9/db7/group__datasets__gr.html#gga610754124ced68d1f05760b5948fbb76a6f0d8b2d9e3e947b2a5c1eff9e81ee95">circle</a>( <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>, p, radius, bvalue, thickness );</div><div class="line">    }</div><div class="line">    <span class="keywordflow">if</span>( flags &amp; FGD_KEY )</div><div class="line">    {</div><div class="line">        fpxls-&gt;push_back(p);</div><div class="line">        <a class="code" href="../../d9/db7/group__datasets__gr.html#gga610754124ced68d1f05760b5948fbb76a6f0d8b2d9e3e947b2a5c1eff9e81ee95">circle</a>( <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>, p, radius, fvalue, thickness );</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> GCApplication::mouseClick( <span class="keywordtype">int</span> event, <span class="keywordtype">int</span> x, <span class="keywordtype">int</span> y, <span class="keywordtype">int</span> flags, <span class="keywordtype">void</span>* )</div><div class="line">{</div><div class="line">    <span class="comment">// TODO add bad args check</span></div><div class="line">    <span class="keywordflow">switch</span>( event )</div><div class="line">    {</div><div class="line">    <span class="keywordflow">case</span> <a name="a30"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#gga927593befdddc7e7013602bca9b079b0ad3419100fc2d7688c6dbe3da030fbfd9">EVENT_LBUTTONDOWN</a>: <span class="comment">// set rect or GC_BGD(GC_FGD) labels</span></div><div class="line">        {</div><div class="line">            <span class="keywordtype">bool</span> isb = (flags &amp; BGD_KEY) != 0,</div><div class="line">                 isf = (flags &amp; FGD_KEY) != 0;</div><div class="line">            <span class="keywordflow">if</span>( rectState == NOT_SET &amp;&amp; !isb &amp;&amp; !isf )</div><div class="line">            {</div><div class="line">                rectState = IN_PROCESS;</div><div class="line">                rect = <a name="a31"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>( x, y, 1, 1 );</div><div class="line">            }</div><div class="line">            <span class="keywordflow">if</span> ( (isb || isf) &amp;&amp; rectState == SET )</div><div class="line">                lblsState = IN_PROCESS;</div><div class="line">        }</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line">    <span class="keywordflow">case</span> <a name="a32"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#gga927593befdddc7e7013602bca9b079b0aa3536f83b6f48da5121041f58fc7a683">EVENT_RBUTTONDOWN</a>: <span class="comment">// set GC_PR_BGD(GC_PR_FGD) labels</span></div><div class="line">        {</div><div class="line">            <span class="keywordtype">bool</span> isb = (flags &amp; BGD_KEY) != 0,</div><div class="line">                 isf = (flags &amp; FGD_KEY) != 0;</div><div class="line">            <span class="keywordflow">if</span> ( (isb || isf) &amp;&amp; rectState == SET )</div><div class="line">                prLblsState = IN_PROCESS;</div><div class="line">        }</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line">    <span class="keywordflow">case</span> <a name="a33"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#gga927593befdddc7e7013602bca9b079b0a59cad7ffe959adf2782a110196d47bf6">EVENT_LBUTTONUP</a>:</div><div class="line">        <span class="keywordflow">if</span>( rectState == IN_PROCESS )</div><div class="line">        {</div><div class="line">            rect = <a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>( <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(rect.x, rect.y), <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a>(x,y) );</div><div class="line">            rectState = SET;</div><div class="line">            setRectInMask();</div><div class="line">            <a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>( bgdPxls.empty() &amp;&amp; fgdPxls.empty() &amp;&amp; prBgdPxls.empty() &amp;&amp; prFgdPxls.empty() );</div><div class="line">            <a class="code" href="../../d7/d84/namespacecvv_1_1impl.html#a5e7f1938e252a7c78ab0fa6751f06808">showImage</a>();</div><div class="line">        }</div><div class="line">        <span class="keywordflow">if</span>( lblsState == IN_PROCESS )</div><div class="line">        {</div><div class="line">            setLblsInMask(flags, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(x,y), <span class="keyword">false</span>);</div><div class="line">            lblsState = SET;</div><div class="line">            <a class="code" href="../../d7/d84/namespacecvv_1_1impl.html#a5e7f1938e252a7c78ab0fa6751f06808">showImage</a>();</div><div class="line">        }</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line">    <span class="keywordflow">case</span> <a name="a34"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#gga927593befdddc7e7013602bca9b079b0aacdda7d94278928c34b61a7bd8be9f62">EVENT_RBUTTONUP</a>:</div><div class="line">        <span class="keywordflow">if</span>( prLblsState == IN_PROCESS )</div><div class="line">        {</div><div class="line">            setLblsInMask(flags, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(x,y), <span class="keyword">true</span>);</div><div class="line">            prLblsState = SET;</div><div class="line">            <a class="code" href="../../d7/d84/namespacecvv_1_1impl.html#a5e7f1938e252a7c78ab0fa6751f06808">showImage</a>();</div><div class="line">        }</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line">    <span class="keywordflow">case</span> <a name="a35"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#gga927593befdddc7e7013602bca9b079b0a8f2ec5fd35fe3f4b696518b146dcf675">EVENT_MOUSEMOVE</a>:</div><div class="line">        <span class="keywordflow">if</span>( rectState == IN_PROCESS )</div><div class="line">        {</div><div class="line">            rect = <a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>( <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(rect.x, rect.y), <a class="code" href="../../db/d4e/classcv_1_1Point__.html">Point</a>(x,y) );</div><div class="line">            <a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>( bgdPxls.empty() &amp;&amp; fgdPxls.empty() &amp;&amp; prBgdPxls.empty() &amp;&amp; prFgdPxls.empty() );</div><div class="line">            <a class="code" href="../../d7/d84/namespacecvv_1_1impl.html#a5e7f1938e252a7c78ab0fa6751f06808">showImage</a>();</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( lblsState == IN_PROCESS )</div><div class="line">        {</div><div class="line">            setLblsInMask(flags, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(x,y), <span class="keyword">false</span>);</div><div class="line">            <a class="code" href="../../d7/d84/namespacecvv_1_1impl.html#a5e7f1938e252a7c78ab0fa6751f06808">showImage</a>();</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( prLblsState == IN_PROCESS )</div><div class="line">        {</div><div class="line">            setLblsInMask(flags, <a class="code" href="../../dc/d84/group__core__basic.html#ga1e83eafb2d26b3c93f09e8338bcab192">Point</a>(x,y), <span class="keyword">true</span>);</div><div class="line">            <a class="code" href="../../d7/d84/namespacecvv_1_1impl.html#a5e7f1938e252a7c78ab0fa6751f06808">showImage</a>();</div><div class="line">        }</div><div class="line">        <span class="keywordflow">break</span>;</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> GCApplication::nextIter()</div><div class="line">{</div><div class="line">    <span class="keywordflow">if</span>( isInitialized )</div><div class="line">        <a name="a36"></a><a class="code" href="../../d3/d47/group__imgproc__segmentation.html#ga909c1dda50efcbeaa3ce126be862b37f">grabCut</a>( *image, <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>, rect, bgdModel, fgdModel, 1 );</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        <span class="keywordflow">if</span>( rectState != SET )</div><div class="line">            <span class="keywordflow">return</span> iterCount;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>( lblsState == SET || prLblsState == SET )</div><div class="line">            <a class="code" href="../../d3/d47/group__imgproc__segmentation.html#ga909c1dda50efcbeaa3ce126be862b37f">grabCut</a>( *image, <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>, rect, bgdModel, fgdModel, 1, <a name="a37"></a><a class="code" href="../../d7/d1b/group__imgproc__misc.html#ggaf8b5832ba85e59fc7a98a2afd034e558ab01527c7effb50fd1c54d8c4e671ed22">GC_INIT_WITH_MASK</a> );</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">            <a class="code" href="../../d3/d47/group__imgproc__segmentation.html#ga909c1dda50efcbeaa3ce126be862b37f">grabCut</a>( *image, <a class="code" href="../../da/dd3/group__gapi__math.html#gaba076d51941328cb7ca9348b7b535220">mask</a>, rect, bgdModel, fgdModel, 1, <a name="a38"></a><a class="code" href="../../d7/d1b/group__imgproc__misc.html#ggaf8b5832ba85e59fc7a98a2afd034e558a5f8853c1e5a89c4aa2687d1f78a7e550">GC_INIT_WITH_RECT</a> );</div><div class="line"></div><div class="line">        isInitialized = <span class="keyword">true</span>;</div><div class="line">    }</div><div class="line">    iterCount++;</div><div class="line"></div><div class="line">    bgdPxls.clear(); fgdPxls.clear();</div><div class="line">    prBgdPxls.clear(); prFgdPxls.clear();</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> iterCount;</div><div class="line">}</div><div class="line"></div><div class="line">GCApplication gcapp;</div><div class="line"></div><div class="line"><span class="keyword">static</span> <span class="keywordtype">void</span> on_mouse( <span class="keywordtype">int</span> event, <span class="keywordtype">int</span> x, <span class="keywordtype">int</span> y, <span class="keywordtype">int</span> flags, <span class="keywordtype">void</span>* param )</div><div class="line">{</div><div class="line">    gcapp.mouseClick( event, x, y, flags, param );</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv )</div><div class="line">{</div><div class="line">    <a name="_a39"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">cv::CommandLineParser</a> parser(argc, argv, <span class="stringliteral">&quot;{@input| messi5.jpg |}&quot;</span>);</div><div class="line">    help(argv);</div><div class="line"></div><div class="line">    <span class="keywordtype">string</span> filename = parser.<a name="a40"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html#a190215863579a6d244dccfd261ee0224">get</a>&lt;<span class="keywordtype">string</span>&gt;(<span class="stringliteral">&quot;@input&quot;</span>);</div><div class="line">    <span class="keywordflow">if</span>( filename.empty() )</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;\nDurn, empty filename&quot;</span> &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> 1;</div><div class="line">    }</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> image = <a name="a41"></a><a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>(<a name="a42"></a><a class="code" href="../../d6/dba/group__core__utils__samples.html#ga3a33b00033b46c698ff6340d95569c13">samples::findFile</a>(filename), <a name="a43"></a><a class="code" href="../../d8/d6a/group__imgcodecs__flags.html#gga61d9b0126a3e57d9277ac48327799c80af660544735200cbe942eea09232eb822">IMREAD_COLOR</a>);</div><div class="line">    <span class="keywordflow">if</span>( image.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() )</div><div class="line">    {</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;\n Durn, couldn&#39;t read image filename &quot;</span> &lt;&lt; filename &lt;&lt; endl;</div><div class="line">        <span class="keywordflow">return</span> 1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">string</span> winName = <span class="stringliteral">&quot;image&quot;</span>;</div><div class="line">    <a name="a44"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>( winName, <a name="a45"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3acf621ace7a54954cbac01df27e47228f">WINDOW_AUTOSIZE</a> );</div><div class="line">    <a name="a46"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga89e7806b0a616f6f1d502bd8c183ad3e">setMouseCallback</a>( winName, on_mouse, 0 );</div><div class="line"></div><div class="line">    gcapp.setImageAndWinName( image, winName );</div><div class="line">    gcapp.showImage();</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span>(;;)</div><div class="line">    {</div><div class="line">        <span class="keywordtype">char</span> c = (char)<a name="a47"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(0);</div><div class="line">        <span class="keywordflow">switch</span>( c )</div><div class="line">        {</div><div class="line">        <span class="keywordflow">case</span> <span class="stringliteral">&#39;\x1b&#39;</span>:</div><div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;Exiting ...&quot;</span> &lt;&lt; endl;</div><div class="line">            <span class="keywordflow">goto</span> exit_main;</div><div class="line">        <span class="keywordflow">case</span> <span class="charliteral">&#39;r&#39;</span>:</div><div class="line">            cout &lt;&lt; endl;</div><div class="line">            gcapp.reset();</div><div class="line">            gcapp.showImage();</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        <span class="keywordflow">case</span> <span class="charliteral">&#39;n&#39;</span>:</div><div class="line">            <span class="keywordtype">int</span> iterCount = gcapp.getIterCount();</div><div class="line">            cout &lt;&lt; <span class="stringliteral">&quot;&lt;&quot;</span> &lt;&lt; iterCount &lt;&lt; <span class="stringliteral">&quot;... &quot;</span>;</div><div class="line">            <span class="keywordtype">int</span> newIterCount = gcapp.nextIter();</div><div class="line">            <span class="keywordflow">if</span>( newIterCount &gt; iterCount )</div><div class="line">            {</div><div class="line">                gcapp.showImage();</div><div class="line">                cout &lt;&lt; iterCount &lt;&lt; <span class="stringliteral">&quot;&gt;&quot;</span> &lt;&lt; endl;</div><div class="line">            }</div><div class="line">            <span class="keywordflow">else</span></div><div class="line">                cout &lt;&lt; <span class="stringliteral">&quot;rect must be determined&gt;&quot;</span> &lt;&lt; endl;</div><div class="line">            <span class="keywordflow">break</span>;</div><div class="line">        }</div><div class="line">    }</div><div class="line"></div><div class="line">exit_main:</div><div class="line">    <a name="a48"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga851ccdd6961022d1d5b4c4f255dbab34">destroyWindow</a>( winName );</div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:29 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
